package cn.cheln.explorer.ds;

import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;

import cn.cheln.explorer.ds.database.SQLManager;

public class OtherFileProvider extends ContentProvider {

    private static final String TABLE_NAME = "files";
    private static final int TABLES = 1;
    private static final int TABLE_ID = 2;
    private static final int IMAGE = 3;
    private static final UriMatcher sUriMatcher;
    private static final int FOLDER = 5;

    static {
        sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);
        sUriMatcher.addURI(OtherFile.OtherFileColumns.AUTHORITY, "files",
                TABLES);
        sUriMatcher
                .addURI(OtherFile.OtherFileColumns.AUTHORITY, "image", IMAGE);
        sUriMatcher.addURI(OtherFile.OtherFileColumns.AUTHORITY, "dir", FOLDER);
        sUriMatcher.addURI(OtherFile.OtherFileColumns.AUTHORITY,
                "files" + "/#", TABLE_ID);
    }

    ;
    int i = 1;
    DatabaseHelper mOpenHelper;
    SQLiteDatabase db;

    @Override
    public int delete(Uri uri, String arg1, String[] arg2) {
        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        String table = getTable(uri);
        return db.delete(table, arg1, arg2);
    }

    @Override
    public String getType(Uri uri) {
        switch (sUriMatcher.match(uri)) {
            case TABLES:
                return OtherFile.OtherFileColumns.CONTENT_TYPE;
            case TABLE_ID:
                return OtherFile.OtherFileColumns.CONTENT_ITEM_TYPE;
            case IMAGE:
                return OtherFile.OtherFileColumns.CONTENT_TYPE_IMAGE;
            case FOLDER:
                return OtherFile.OtherFileColumns.CONTENT_TYPE_FOLDER;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
    }

    @Override
    public Uri insert(Uri uri, ContentValues initialValues) {
        // TODO Auto-generated method stub
        String table = getTable(uri);
        ContentValues values;
        if (initialValues != null) {
            values = new ContentValues(initialValues);
        } else {
            values = new ContentValues();
        }
//        Logs.i("============= filename " + values.get("_data") + " " + values);

        SQLiteDatabase db = mOpenHelper.getWritableDatabase();
        long rowId = db.insert(table, null, values);
//        db.close();
        if (rowId > 0) {
            Uri myUri = ContentUris.withAppendedId(uri, rowId);
            return myUri;
        }

        throw new SQLException("Failed to insert row into " + uri);

    }

    private String getTable(Uri uri) {
        String table = "";
        switch (sUriMatcher.match(uri)) {
            case TABLES:
                table = SQLManager.tablename;
                break;
            case IMAGE:
                table = SQLManager.tableimage;
                break;
            case FOLDER:
                table = SQLManager.tabledir;
                break;
            default:
                throw new IllegalArgumentException("Unknown URI " + uri);
        }
        return table;
    }

    @Override
    public boolean onCreate() {
        // TODO Auto-generated method stub
        mOpenHelper = new DatabaseHelper(getContext());
        return true;
    }

    public SQLiteDatabase getDatabase() {
        if(db==null||!db.isOpen()){
            db = mOpenHelper.getReadableDatabase();
        }
        return db;
    }

    @Override
    public Cursor query(Uri uri, String[] projection, String selection,
                        String[] selectionArgs, String sortOrder) {
        SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
        String table = getTable(uri);
        qb.setTables(table);
        SQLiteDatabase db = getDatabase();
        Cursor c = qb.query(db, projection, selection,
                selectionArgs, null, null, sortOrder);
//        db.close();
        return c;
    }

    @Override
    public int update(Uri uri, ContentValues values, String selection,
                      String[] selectionArgs) {
        String table = getTable(uri);

        SQLiteDatabase db = getDatabase();
        int result = db.update(table, values, selection, selectionArgs);
        return result;
    }

}
